﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 站点 添加 修改 
//最后修改: 2010-10-11

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Collections;

namespace TRPOP.CMS.Web.Admin
{
    public partial class PSN_save : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_PSN;
        }
        #endregion


        #region 页面变量
        protected bool m_IsAdd = true;
        protected int m_id = 0;
        protected string m_Title = "";
        PSNInfo mPSNInfo = new PSNInfo();
        PSNManage mPSNManage = new PSNManage();
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            
            string strMSG = "站点名称 、域名、目录 或描述";
            if (!PageAssist.CheckForm(txtName, ref strMSG) ||
                !PageAssist.CheckForm(txtUrl, ref strMSG) ||
                !PageAssist.CheckForm(txtRoot, ref strMSG)
                )
            {
                Alert(strMSG);
                return;
            }

            int.TryParse(GetRequestViewState("id"), out m_id);
            PSNInfo OldPSNInfo = null;
            if (m_id > 0)
            {
                mPSNInfo = mPSNManage.GetInfoById(m_id);

                if (mPSNInfo == null)
                {
                    mPSNInfo = new PSNInfo();
                }
                else
                {
                    OldPSNInfo = (PSNInfo)GetPageViewState("model");
                }

            }
            mPSNInfo.Name = txtName.TextTrim.RemoveHtml();
            mPSNInfo.Type = ddlType.SelectedValue.ToInt();
            //mPSNInfo.TimeZoneId = ddlTime.SelectedValue.ToInt();
            mPSNInfo.Url = txtUrl.Text;
            mPSNInfo.Root = txtRoot.Text;

            if (mPSNInfo.Root.IndexOf("/") == 0)
            {
                if (!FileUtil.CheckPathAndCreate(Common.Util.GetMapPath(mPSNInfo.Root), true))
                {
                    Alert("保存失败，\n原因：目录不存在，创建目录出错。");
                    return;
                }
            }

            mPSNInfo.Description = txtDescription.Text;
            mPSNInfo.Xml = "";//txtXML.Text;
            
            if (OldPSNInfo != null)
            {
                while (mPSNInfo.Url.Trim().EndsWith("/"))
                {
                    mPSNInfo.Url = mPSNInfo.Url.Trim().Remove(mPSNInfo.Url.Trim().Length - 1);
                }

                if (OldPSNInfo.Url != mPSNInfo.Url)
                {
                    mPSNManage.UpdateUrl(OldPSNInfo, mPSNInfo);
                }

                System.Threading.Thread oThread = new System.Threading.Thread(this.UpdateAllFileURL);
                oThread.Start();
            }

            EnumSubmitResult enumResult = mPSNManage.Save(mPSNInfo);
            if (m_id > 0)
            {
                base.SaveLog(Plugin.LogKeys.OperObject.PSNOper, Plugin.LogKeys.OperResult.EditSubmit, m_id.ToString());
            }
            else
            {
                base.SaveLog(Plugin.LogKeys.OperObject.PSNOper, Plugin.LogKeys.OperResult.AddSubmit, mPSNInfo.psnid.ToString());
            }
            SubmitAlertAndThisNewLocation(enumResult, "PSN_list.aspx");

        }

        protected void UpdateAllFileURL()
        {
            NodeManage oNodeManage = new NodeManage();
            ContentUpFileManage oContentUpFileManage = new ContentUpFileManage();

            SerializableDictionary<string, string> oDictionary = new SerializableDictionary<string, string>();

            foreach (NodeInfo oNodeInfo in oNodeManage.GetNodeListByParentID(0))
            {
                oDictionary = oNodeManage.SaveNodeURLForSourcePSN(oNodeInfo);
                if (oDictionary.ContainsKey(oNodeInfo.NodeId.ToString() + "_PSNSourceRoot"))
                {
                    oContentUpFileManage.UpdateFileURL(oNodeInfo.NodeId, oDictionary[oNodeInfo.NodeId.ToString() + "_PSNSourceRoot"], oDictionary[oNodeInfo.NodeId.ToString() + "_PSNSourceRootURL"]);
                }
            }

            oDictionary = oNodeManage.SaveNullNodeURLForSourcePSN();
            if (oDictionary.ContainsKey("0_PSNSourceRoot"))
            {
                oContentUpFileManage.UpdateFileURL(0, oDictionary["0_PSNSourceRoot"], oDictionary["0_PSNSourceRootURL"]);
            }

            try
            {
                System.Threading.Thread.CurrentThread.Abort();
            }
            catch
            {

            }
        }
        #endregion

        #region 页面方法

        public void LoadBase()
        {
            int.TryParse(InitRequestViewState("id"), out m_id);
            if (m_id > 0)
            {
                mPSNInfo = mPSNManage.GetInfoById(m_id);

                if (mPSNInfo != null)
                {
                    InitPageViewState("model", mPSNInfo);
                    m_IsAdd = false;
                    m_Title = mPSNInfo.Name;
                    txtName.Text = mPSNInfo.Name;
                    ddlType.SelectedValue = mPSNInfo.Type.ToString();
                    //ddlTime.SelectedValue = mPSNInfo.TimeZoneId.ToString();
                    txtUrl.Text = mPSNInfo.Url;
                    hidURL.Value = mPSNInfo.Url;
                    txtRoot.Text = mPSNInfo.Root;
                    txtDescription.Text = mPSNInfo.Description;
                    txtXML.Text = mPSNInfo.Xml.ToString();
                }
            }
            else
                base.AlertForNoPermissionAddPSN(mPSNManage.IsOverFlow());
        }
        public void BindData()
        {
            ddlType.DataSource = PageAssist.GetAllPSNType(SystemInfoHelper.getSystemInfo().AdminFolderName);
            ddlType.DataBind();

            //TimeZoneManage oTimeZoneManage = new TimeZoneManage();
            //ddlTime.DataSource = oTimeZoneManage.GetListByCondition(0, "", "TimeZoneId");
            //ddlTime.DataBind();
        }
        #endregion

    }
}
